<?php
namespace Tongcheng\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function _initialize(){
        //获得模板消息
//        getTemplate();

//        sendOrderMessage("oRVj50L-3jTgEqslDeC7sz8lc-Ds","2018050450100985");
//        var_dump($a);exit;
//    	 dump($_SESSION);

        $url = $_SERVER["REQUEST_URI"];
        session('wid',2);
//        echo $url;
//        echo strpos($url,'Book');
//        exit();
        if(strpos($url,'Book')>0 || strpos($url,'Notify2')>0){
            return;
        }
//        session('wid') = 2;
    	if(!session('?uid')){
    		$this->redirect("Home/Public/login");
    	}
        if($_GET['wid']){
            $map['uid'] = session('uid');
            $map['wid'] = 2;

            if($wechat = M("Wechat")->where($map)->find()){
                if($wechat['end_time'] < date("Y-m-d")){
                    $this->error("小程序已到期，请联系管理员续期");
                }
                if($wechat['status'] == 0){
                    $this->error("该小程序已被限制使用");
                }
                session('wid', $_GET['wid']);
                session('wechat.nickname', $wechat['nick_name']);
                session('wechat.headimg', $wechat['head_img']);
            }else{
                $this->error('请选择要管理的小程序',U('Home/Index/index'));
            }
        }
        if(!session('?wid')){
            $this->error('请选择要管理的小程序',U('Home/Index/index'));
        }
    }


    protected function ihttp_post($url, $post_data){
    	$ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    protected function ihttp_get($url, $is_https = true){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($is_https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }
}